﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Web.SystemConfig.WorkFlow;
using Web.SystemLogin.Models;
using Wuqi.Webdiyer;
using Web.SystemConfig.SysCache;
using System.Collections;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellDataView : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string m_JobType = string.Empty;
            if (!IsPostBack)
            {
                switch (Request.QueryString["jobType"])
                {
                    case "1":
                        m_JobType = "1";
                        break;
                    case "2":
                        m_JobType = "2";
                        break;
                    default:
                        m_JobType = "1";
                        break;
                }

                this.SetControl(m_JobType);
                this.BindData(m_JobType);
            }
        }

        private void SetControl(string m_JobType)
        {
            switch (m_JobType)
            {
                case "1":
                    this.divDone.Visible = false;
                    this.divAgent.Visible = true;
                    break;
                case "2":
                    this.divDone.Visible = true;
                    this.divAgent.Visible = false;
                    break;
                default:
                    this.divDone.Visible = false;
                    this.divAgent.Visible = true;
                    break;
            }
        }

        private void BindData(string jobType)
        {
            if (jobType == "1")
            {
                this.BindAgent();
            }
            else
            {
                BindDone();
            }
        }

        private void BindAgent()
        {
            int sum = 0;
            DataTable oDataTable = null;
            WorkFlowDataAccess oWorkFlowDataAccess = new WorkFlowDataAccess();
            int pageSize = this.gv_SingleWellAgent.PageSize;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            if (currentUser == null)
            {
                throw new Exception("获取当前登陆用户失败");
            }
            oDataTable = oWorkFlowDataAccess.GetAgent(ArchiveTableEnum.SingleWell, "singlewell", currentUser.User_ID, 0, 100000, "statusID=2", ref sum);
            int recordCount = 0;
            this.gv_SingleWellAgent.DataSource = PageDataList.GetPagedTable(oDataTable, this.pager_SingleWellAgent.CurrentPageIndex, this.pager_SingleWellAgent.PageSize, out recordCount);
            this.pager_SingleWellAgent.RecordCount = oDataTable.Rows.Count;
            this.gv_SingleWellAgent.DataBind();

            this.btnAgree.Visible = true;
            this.btnDisAgree.Visible = true;
        }

        private void BindDone()
        {
            int sum = 0;
            DataTable oDataTable = null;
            WorkFlowDataAccess oWorkFlowDataAccess = new WorkFlowDataAccess();
            int pageSize = this.gv_SingleWellAgent.PageSize;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            if (currentUser == null)
            {
                throw new Exception("获取当前登陆用户失败");
            }
            oDataTable = oWorkFlowDataAccess.GetDone(ArchiveTableEnum.SingleWell, "singlewell", currentUser.User_ID, 0, 100000, "statusID in (1,3)", ref sum);
            int recordCount = 0;
            this.gv_SingleWellDone.DataSource = PageDataList.GetPagedTable(oDataTable, this.pager_SingleWellDone.CurrentPageIndex, this.pager_SingleWellDone.PageSize, out recordCount);
            this.pager_SingleWellDone.RecordCount = oDataTable.Rows.Count;
            this.gv_SingleWellDone.DataBind();
        }

        protected void pager_SingleWellAgent_PageChanging(object sender, PageChangingEventArgs e)
        {
            pager_SingleWellAgent.CurrentPageIndex = e.NewPageIndex;
            BindAgent();
        }

        protected void pager_SingleWellDone_PageChanging(object sender, PageChangingEventArgs e)
        {
            pager_SingleWellDone.CurrentPageIndex = e.NewPageIndex;
            BindDone();
        }

        #region 按钮事件
        protected void btnAgree_Click(object sender, EventArgs e)
        {
            List<Guid> selectDataList = this.CollectSelectData();
            if (selectDataList != null && selectDataList.Count > 0)
            {
                foreach (Guid recordID in selectDataList)
                {
                    WorkFlowProcessAgree(recordID);
                }
                MyAlert("审核数据成功", "SingleWellExameView.aspx?jobtype=1");
            }
            else
            {
                Alert("请选择数据后再进行操作！");
            }
        }

        protected void btnDisAgree_Click(object sender, EventArgs e)
        {
            List<Guid> selectDataList = this.CollectSelectData();
            if (selectDataList != null && selectDataList.Count > 0)
            {
                foreach (Guid recordID in selectDataList)
                {
                    WorkFlowProcessDisAgree(recordID);
                }
                MyAlert("审核数据成功", "SingleWellExameView.aspx?jobtype=1");
            }
            else
            {
                Alert("请选择数据后再进行操作！");
            }
        }
        #endregion

        #region 流程操作

        private List<Guid> CollectSelectData()
        {
            List<Guid> rtnRecordList = null;
            ArrayList recordIDList = this.gv_SingleWellAgent.SelectItems;
            if (recordIDList != null && recordIDList.Count > 0)
            {
                rtnRecordList = new List<Guid>();
                foreach (object obj in recordIDList)
                {
                    rtnRecordList.Add(new Guid(obj.ToString()));
                }
            }

            return rtnRecordList;
        }

        private void WorkFlowProcessDisAgree(Guid recordID)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.CommentMessage = "批量审核";
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.SingleWell;
            oWorkFlowModel.RecordID = recordID;
            oWorkFlowModel.FromStatusID = 2;
            WorkFlowMessageStatusRule CurrentRule = new ArchiveCacheFacade().GetStatusRuleCache().GetRuleByStatusIDAndOperateName(2, "不同意");
            oWorkFlowModel.OperateID = CurrentRule.OperateID;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            //先签收数据
            oWorkFlow.SignData(oWorkFlowModel);
            oWorkFlow.FlowProcess(oWorkFlowModel);
        }

        private void WorkFlowProcessAgree(Guid recordID)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            WorkFlowModel oWorkFlowModel = new WorkFlowModel();
            oWorkFlowModel.OperateLogID = Guid.NewGuid();
            oWorkFlowModel.CommentMessage = "批量审核";
            oWorkFlowModel.TableID = (int)ArchiveTableEnum.SingleWell;
            oWorkFlowModel.RecordID = recordID;
            oWorkFlowModel.FromStatusID = 2;
            WorkFlowMessageStatusRule CurrentRule = new ArchiveCacheFacade().GetStatusRuleCache().GetRuleByStatusIDAndOperateName(2, "同意");
            oWorkFlowModel.OperateID = CurrentRule.OperateID;
            Web.SystemLogin.Models.User currentUser = (Web.SystemLogin.Models.User)Session["CurrentUser"];
            oWorkFlowModel.FromUserID = new Guid(currentUser.User_ID);
            //先签收数据
            oWorkFlow.SignData(oWorkFlowModel);
            oWorkFlow.FlowProcess(oWorkFlowModel);
        }

        #endregion
    }
}